﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;

namespace UncleBobWebService.Database
{
    /// <summary>
    /// The purpose of this class is to allow the program to register, validate, query, and edit registered information.
    /// </summary>
    class RegisterDBAdapter : IDBAdapter
    {
        /// <summary>
        /// This method registers a user.
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public override Message CreateData(Message msg, Object user)
        {
            RegisterModel model = (RegisterModel)user;
            Message newMsg = new Message();
            newMsg.Data = model;

            RegisterModel exitingModel = (RegisterModel)SearchForAllData(newMsg).Data;

            if (exitingModel.Username != model.Username)
            {

                using (SqlConnection connection = new SqlConnection(mConnectionString))
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = string.Format("INSERT INTO UserRegistration (UserName, Password, FirstName, MI, LastName, Address, PhoneNumber) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')"
                        , model.Username, model.Password, model.FirstName, model.MI, model.LastName, model.Address, model.PhoneNumber);

                    try
                    {
                        connection.Open();
                        command.ExecuteNonQuery();
                        msg = new Message();
                        msg.Result = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            return msg;
        }

        /// <summary>
        /// This method validates a user.
        /// </summary>
        /// <param name="msg">Message object</param>
        /// <param name="data">A RegisterModel</param>
        /// <returns></returns>
        public override Message SearchForSpecificData(Message msg, Object data)
        {
            SqlDataAdapter SqlAdapter;
            DataTable dt = new DataTable();
            
            using (SqlConnection connection = new SqlConnection(mConnectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = string.Format("SELECT * FROM UserRegistration WHERE UserName = '{0}' AND Password = '{1}'", 
                    ((RegisterModel)data).Username, ((RegisterModel)data).Password);
                SqlAdapter = new SqlDataAdapter(command);

                try
                {
                    connection.Open();
                    SqlAdapter.Fill(dt);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                if (dt.Rows.Count != 0)
                {
                    if (string.Compare(dt.Rows[0]["Password"].ToString(), ((RegisterModel)data).Password) == 0)
                    {
                        msg = new Message();
                        msg.Result = true;
                    }
                }
            }

            return msg;
        }

        /// <summary>
        /// This method returns a user.
        /// </summary>
        /// <param name="msg">Message which contains a RegisterModel in the data section.</param>
        /// <returns></returns>
        public override Message SearchForAllData(Message msg)
        {
            RegisterModel regModel = new RegisterModel();
            SqlDataAdapter SqlAdapter;
            DataTable dt = new DataTable();
            Message newMsg = new Message();

            using (SqlConnection connection = new SqlConnection(mConnectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = string.Format("SELECT * FROM UserRegistration WHERE UserName = '{0}'", ((RegisterModel)msg.Data).Username);
                SqlAdapter = new SqlDataAdapter(command);

                try
                {
                    connection.Open();
                    SqlAdapter.Fill(dt);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                if (dt.Rows.Count != 0)
                {
                    regModel.Address = dt.Rows[0]["Address"].ToString();
                    regModel.FirstName = dt.Rows[0]["FirstName"].ToString();
                    regModel.LastName = dt.Rows[0]["LastName"].ToString();
                    regModel.MI = dt.Rows[0]["MI"].ToString();
                    regModel.Password = dt.Rows[0]["Password"].ToString();
                    regModel.PhoneNumber = dt.Rows[0]["PhoneNumber"].ToString();
                    regModel.Username = dt.Rows[0]["UserName"].ToString();
                    regModel.Status = (bool)dt.Rows[0]["Status"];
                    newMsg.Data = regModel;
                    newMsg.Result = true;
                }
            }

            return newMsg;
        }

        /// <summary>
        /// This method allows you to edit a user.
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public override ServiceRequester.Message EditSpecificData(ServiceRequester.Message msg, Object data)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// This method deletes a user
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public override Message DeleteSpecificData(Message msg, Object data)
        {
            Message newMsg = new Message();  
            if (SearchForSpecificData(newMsg, data).Result  == true)
            {
                SqlDataAdapter SqlAdapter;
                DataTable dt = new DataTable();

                using (SqlConnection connection = new SqlConnection(mConnectionString))
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = string.Format("DELETE FROM UserRegistration WHERE UserName = '{0}'", ((RegisterModel)data).Username);
                    SqlAdapter = new SqlDataAdapter(command);

                    try
                    {
                        connection.Open();
                        SqlAdapter.Fill(dt);
                        msg = new Message();
                        msg.Result = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            return msg;            
        }

        /// <summary>
        /// NOT USED AT THIS TIME.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override ServiceRequester.Message DeleteAllData(ServiceRequester.Message msg)
        {
            throw new NotImplementedException();
        }
    }
}
